package gameCreation;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class questionEditor {

	JFrame frame;
	int catNum, btnNum, value;
	GameCreator theGame;

	/**
	 * Create the application.
	 */
	public questionEditor(int cat, int num, int value, GameCreator g) {
		catNum = cat;
		btnNum = num;
		this.value = value;
		theGame = g;
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 425, 402);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel lblEditQuestion = new JLabel("Edit question");
		lblEditQuestion.setFont(new Font("Tahoma", Font.PLAIN, 23));
		lblEditQuestion.setBounds(133, 11, 132, 64);
		frame.getContentPane().add(lblEditQuestion);
		
		JPanel panel = new JPanel();
		panel.setBounds(10, 80, 392, 275);
		frame.getContentPane().add(panel);
		panel.setLayout(null);
		
		JCheckBox chckbxDailyDouble = new JCheckBox("Daily Double?");
		chckbxDailyDouble.setBounds(10, 183, 155, 23);
		panel.add(chckbxDailyDouble);
		
		JLabel lblQuestion = new JLabel("Question:");
		lblQuestion.setBounds(10, 11, 86, 14);
		panel.add(lblQuestion);
		
		JTextArea txtQuestion = new JTextArea();
		txtQuestion.setLineWrap(true);
		txtQuestion.setWrapStyleWord(true);
		
		if(theGame.isDoubleJeopardy()){    //Sets the Q&A fields so the don't reset
			if(theGame.doubleQuestions[catNum][btnNum] != null){
				txtQuestion.setText(theGame.doubleQuestions[catNum][btnNum].Question);
				chckbxDailyDouble.setSelected(theGame.doubleQuestions[catNum][btnNum].dailyDouble);
			}
		}
		else{
			if(theGame.normalQuestions[catNum][btnNum] != null){
				txtQuestion.setText(theGame.normalQuestions[catNum][btnNum].Question);
				chckbxDailyDouble.setSelected(theGame.normalQuestions[catNum][btnNum].dailyDouble);
			}
		}
		
		txtQuestion.setBounds(10, 36, 372, 52);
		panel.add(txtQuestion);
		
		JLabel lblAnswer = new JLabel("Answer:");
		lblAnswer.setBounds(10, 99, 86, 14);
		panel.add(lblAnswer);
		
		JTextArea txtAnswer = new JTextArea();
		txtAnswer.setWrapStyleWord(true);
		txtAnswer.setLineWrap(true);
		
		if(theGame.isDoubleJeopardy()){ //Sets q&A fields so they do no reset
			if(theGame.doubleQuestions[catNum][btnNum] != null){
				txtAnswer.setText(theGame.doubleQuestions[catNum][btnNum].Answer);
				chckbxDailyDouble.setSelected(theGame.doubleQuestions[catNum][btnNum].dailyDouble);
			}
		}
		else{
			if(theGame.normalQuestions[catNum][btnNum] != null){
				txtAnswer.setText(theGame.normalQuestions[catNum][btnNum].Answer);
				chckbxDailyDouble.setSelected(theGame.normalQuestions[catNum][btnNum].dailyDouble);
			}
		}
		
		txtAnswer.setBounds(10, 124, 372, 52);
		panel.add(txtAnswer);
		
		final GameCreator gc = theGame;
		final questionEditor THIS = this;
		final int f_value = value;
		final JTextArea f_question = txtQuestion;
		final JTextArea	f_answer = txtAnswer;
		final JCheckBox f_dailyDouble = chckbxDailyDouble;
		
		JButton btnDone = new JButton("Done");
		btnDone.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(gc.isDoubleJeopardy()){
					int lastValue = f_value;
					if(f_dailyDouble.isSelected())
						lastValue *= 2;
					QData thisQ = new QData(f_question.getText(), f_answer.getText(), lastValue, f_dailyDouble.isSelected());
					gc.doubleQuestions[catNum][btnNum] = thisQ;
					THIS.frame.dispose();
				}
				else{
					int lastValue = f_value;
					if(f_dailyDouble.isSelected())
						lastValue *= 2;
					QData thisQ = new QData(f_question.getText(), f_answer.getText(), lastValue, f_dailyDouble.isSelected());
					gc.normalQuestions[catNum][btnNum] = thisQ;
					THIS.frame.dispose();
				}
				
			}
		});
		btnDone.setBounds(293, 241, 89, 23);
		panel.add(btnDone);
		
		
	}
}
